//
//  WmcAlbumBrowseVM.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/29.
//

import RxRelay
import UIKit
import YPBedrock

public extension WmcMediaBrowseVM {
    /// 相册类型
    enum AlbumType {
        /// 系统
        case system

        /// 本地
        case local

        /// 云端
        case cloud

        /// 自定义
        case custom(tag: String)
    }
}

public extension WmcMediaBrowseVM {
    /// 按钮事件类型
    enum ActionType {
        /// 删除
        case delete

        /// 上传
        case upload

        /// 保存
        case save

        /// 分享
        case share

        /// 分享朋友圈
        case shareFriendsCircle

        /// 分享微信
        case shareWechat
    }

    /// 左上角样式
    enum TopLeftStyle {
        /// 关闭
        case close

        /// 关闭带标题
        case closeTitle(title: String)

        /// 返回
        case back

        /// 返回带标题
        case backTitle(title: String)
    }

    /// 右上角样式
    enum TopRightStyle {
        /// 无
        case none

        /// 图片计数
        case imageCount

        /// 全部照片
        case allPhoto
    }

    /// 分享模式
    enum ShareMode {
        /// 默认
        case `default`

        /// 自定义
        case custom
    }
}

// MARK: - [WmcMediaBrowseVM]

open class WmcMediaBrowseVM: WmcBaseVM {
    // MARK: 入参

    /// 相册类型，不同类型不同展示
    var albumType: AlbumType = .system

    /// 左上角样式
    var topLeftStyle: TopLeftStyle = .close

    /// 右上角样式
    var topRightStyle: TopRightStyle = .imageCount

    /// 展示的索引
    var showIndex: Int = 0

    /// 返回样式
    var backStyle: WmcTransition.Style = .bottom

    /// 分享模式
    var shareMode: ShareMode = .default

    /// 分享朋友圈模式
    var shareFriendsCircleMode: ShareMode = .default

    /// 分享微信模式
    var shareWechatMode: ShareMode = .default

    /// 设置底部按钮列表
    var tooMenuList: [WmcButtonItemType.MediaBrowseTool]? = nil
    
    // MARK: 输出

    /// 照片数组更新
    var mediaListUpdateRelay: PublishRelay<[WmcMedia]> = .init()

    /// 空时返回
    let mediaListEmptyRelay: PublishRelay<Void> = .init()

    /// 设置底部按钮列表
    var toolMenuListRelay: PublishRelay<([WmcButtonItemType.MediaBrowseTool], WmcMedia.MediaType)> = .init()
    
    // MARK: 回调

    /// Block事件
    var actionBlock: YPBlock.ThreeParam<ActionType, Int, WmcMediaBrowseVM>?

    /// 获取当前媒体的水印信息
    var currentMediaWatermarkBlock: YPBlock.OneParamReturn<Int, WmcWatermarkDetailModel?>?

    // MARK: 其他

    /// 当前索引
    var currentIndex: Int = -1

    // MARK: 入参

    /// 照片数组
    var mediaList: [WmcMedia] = [] {
        didSet { reloadRelay() }
    }
    
    /// 数据源
    var dataSourceList: [WmcLocalMediaAlbumDBManager.DBModel]?

    func loadMenuListData(mediaType: WmcMedia.MediaType) {
        var defaultCaseList: [WmcButtonItemType.MediaBrowseTool] = []
        if let menuList = tooMenuList {
            defaultCaseList = menuList
        }else{
            switch albumType {
            case .system,
                 .local:
                switch mediaType {
                case .image: // 图片才有编辑
                    defaultCaseList = [
                        .edit,
                        .delete,
                        .upload,
                        .shared,
                    ]
                case .video: // 视频没有朋友圈分享
                    defaultCaseList = [
                        .delete,
                        .upload,
                        .wechat,
                    ]
                default: break
                }
            case .cloud:
                defaultCaseList = [
                    .delete,
                    .save,
                    .friendsCircle,
                    .wechat,
                ]
            case .custom:
                defaultCaseList = []
            }
        }
        toolMenuListRelay.accept((defaultCaseList, mediaType))
    }
    
    override func config() {}

    func actionFunction(_ actionType: ActionType, index: Int) {
        actionBlock?(actionType, index, self)
    }

    /// 移除相应索引
    func remove(at index: Int) {
        mediaList.remove(at: index)
        dataSourceList?.remove(at: index)
        reloadRelay()
    }

    /// 弹出删除框
    func presentDeleteAlert(actionBlock: @escaping YPBlock.Empty) {
        if let media = mediaList.yp.object(at: currentIndex) {
            switch media.mediaType() {
            case .video:
                switch albumType {
                case .cloud:
                    presentAlert(msg: "确定从云相册中删除这个视频吗？", actionBlock: actionBlock)
                case .local:
                    presentAlert(msg: "确定删除这个视频吗？", actionBlock: actionBlock)
                default: break
                }
            case .image:
                switch albumType {
                case .cloud:
                    presentAlert(msg: "确定从云相册中删除这张照片吗？", actionBlock: actionBlock)
                case .local:
                    presentAlert(msg: "确定删除这张图片吗？", actionBlock: actionBlock)
                default: break
                }
            default: break
            }
        }
    }

    /// 弹出删除框
    func presentAlert(msg: String,
                      actionBlock: @escaping YPBlock.Empty)
    {
        let alert = WmcAlertView()
        alert.msg = msg
        alert.title = ""
        alert.contentLabel.textColor = .wmc_grayH2_323233
        alert.contentLabel.font = 18.yp.uiFont(weight: .bold)
        alert.contentLabel.snp.remakeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.bottom.equalTo(alert.horizontalLine.snp.top)
        }
        alert.style = .custom(confirmTitle: "删除", cancelTitle: "取消")
        alert.confirmBlock = {
            actionBlock()
        }
        alert.show(height: 124)
    }

    /// 刷新输出
    func reloadRelay() {
        mediaListUpdateRelay.accept(mediaList)
        if mediaList.count <= 0 { // 空了
            mediaListEmptyRelay.accept(())
        }
    }
}
